package com.controller;

import com.dao.IDeptDAO;
import com.dao.IEmpDAO;
import com.util.SessionUtil;
import com.vo.Dept;
import com.vo.Emp;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 鲁昊天
 * @date 2024/12/16
 */
@Controller
public class DeptController {

    IDeptDAO deptDAO = null;
    SqlSession session = null;
    @RequestMapping("selectAllDept")
    public String selectAllEmp(Model model) throws Exception {
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            deptDAO = session.getMapper(IDeptDAO.class);//装载接口类
            List<Dept> depts = deptDAO.selectAll(); //调用接口中 定义的数据库操作方法
            model.addAttribute("depts", depts);
            //session.commit();  如果遇到 insert delete update 必须执行commit
        }
        sessionUtil.destory(); //关闭session
        return "showDepts.jsp";
    }
    @RequestMapping("delDept/{id}")
    public String delEmp(@PathVariable("id") int deptno) throws Exception {
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            deptDAO = session.getMapper(IDeptDAO.class);//装载接口类
            int result = deptDAO.delDept(deptno); //调用接口中 定义的数据库操作方法
            session.commit();  //如果遇到 insert delete update 必须执行commit
            if (result == 1) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
        sessionUtil.destory(); //关闭session
        return  "/selectAllDept";
    }
    @RequestMapping("findDept/{id}")
    public String findEmpById(@PathVariable("id") int deptno, HttpSession session1){
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            deptDAO = session.getMapper(IDeptDAO.class);//装载接口类
            Dept dept = deptDAO.findDeptById(deptno); //调用接口中 定义的数据库操作方法
            session1.setAttribute("dept", dept);
            //session.commit();  如果遇到 insert delete update 必须执行commit
        }
        sessionUtil.destory(); //关闭session
        return "/Dedit.jsp";
    }
    @RequestMapping("updateDept")
    public String updateEmp(Dept dept){
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            deptDAO = session.getMapper(IDeptDAO.class);//装载接口类
            int result = deptDAO.updateDept(dept); //调用接口中 定义的数据库操作方法
            session.commit();  //如果遇到 insert delete update 必须执行commit
            if (result == 1) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
        sessionUtil.destory(); //关闭session
        return "/selectAllDept";
    }
}
